package cn.boot.action;

import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.boot.common.BaseAction;
import cn.boot.entity.Draft;
import cn.boot.service.BizService;
import util.Constant;
import util.FieldUtil;
import util.MessageUtil;
import util.Page;

@Controller
@RequestMapping("/sys")
public class DraftAction extends BaseAction {
	private String			actionname	= "草稿";
	private String			actionclass	= "Draft";
	@Autowired
	private BizService	service;

	@RequestMapping(value = "/add2Draft.do")
	public String add2() {
		request.setAttribute("actionname", actionname);
		request.setAttribute("actionclass", actionclass);
		return "addDraft";
	}

	@RequestMapping(value = "/getDraft.do")
	public String get(int uid) {
		try {
			Draft temp = service.get(Draft.class, uid);
			request.setAttribute("modifybean", temp);
			
			List<?> list = service.queryByHQL("from Contact where user.id=?", getSimpleUser().getId());
			putRequestValue("clist", list);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "draft";
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "获取信息失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/addDraft.do")
	public String add(Draft bean) {
		try {
			service.add(bean);
			MessageUtil.addMessage(request, "添加成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "添加失败.");
			return ERROR;
		}

	}

	@RequestMapping(value = "/updateDraft.do")
	public String update(Draft bean) {
		try {
			service.update(bean);
			MessageUtil.addMessage(request, "更新成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "更新失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/deleteDraft.do")
	public String delete(String ids) {
		try {
			service.delete(Draft.class, ids);
			MessageUtil.addRelMessage(request, "操作成功.", "mainquery");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "操作失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/queryDraft.do")
	public String query() {
		try {
			// 字段名称集合
			LinkedList<String> parmnames = new LinkedList<String>();
			// 字段值集合
			LinkedList<Object> parmvalues = new LinkedList<Object>();
			Page p = FieldUtil.createPage(request, Draft.class, parmnames, parmvalues);

			parmnames.add("user.id");
			parmvalues.add(getSimpleUser().getId());

			Page page = service.find(p, Draft.class);
			session.setAttribute(Constant.SESSION_PAGE, page);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "listDraft";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

}
